﻿//matcap 
// matcap 高光做出金屬效果
Shader "TxShader/Shader74/shader_74_2"
{
    Properties
    {
        _BaseMap("BaseMap", 2D) = "white" {}
        _MatcapMap("MatcapMap",2D) = "white" {}
        _MaskMap("MaskMap",2D) = "white" {}
        _NormalMap("NormalMap",2D) = "white" {}
    }

     SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            
            CGPROGRAM
            #pragma vertex vert
			#pragma fragment frag
			// make fog work
			#pragma multi_compile_fog
			
			#include "UnityCG.cginc"


            struct Attributes
            {
                float4 positionOS       : POSITION;
                float2 uv               : TEXCOORD0;
                half3 normalOS          : NORMAL;
                half4 tangentOS         : TANGENT;
            };

            struct Varyings
            {
                float4 positionCS       : SV_POSITION;
                float4 uv               : TEXCOORD0;
                half4 normalWS           : TEXCOORD1;
                half4 tangentWS         : TEXCOORD2;
                half4 bitangentWS       : TEXCOORD3;
            };

            half4 _BaseColor;
            float4 _BaseMap_ST;
            float4 _NormalMap_ST;
            sampler2D _BaseMap;
            sampler2D _MatcapMap;
            sampler2D _NormalMap;
            sampler2D _MaskMap;
            // #define smp _linear_clampU_mirrorV
            // SAMPLER(smp);

            Varyings vert(Attributes v)
            {
                Varyings o = (Varyings)0;

                o.positionCS = UnityObjectToClipPos(v.positionOS.xyz);
                o.uv.xy = TRANSFORM_TEX(v.uv, _BaseMap);
                o.uv.zw = TRANSFORM_TEX(v.uv, _NormalMap);
                half3 normalWS = UnityObjectToWorldNormal(v.normalOS.xyz);

                o.normalWS.xyz = normalWS;
                o.tangentWS.xyz = UnityObjectToWorldDir(v.tangentOS.xyz);
                //half sign = v.tangentOS.w * GetOddNegativeScale();
                o.bitangentWS.xyz = cross(o.normalWS.xyz,o.tangentWS.xyz);
                return o;
            }

            half4 frag(Varyings i) : SV_Target
            {
                half4 c;

                half4 baseMap = tex2D(_BaseMap, i.uv.xy);

                half maskMap = tex2D(_MaskMap, i.uv.xy).r;

                //计算法线纹理
                half3 normalMap = UnpackNormal(tex2D(_NormalMap,i.uv.zw));
                half3 normalWS = mul(normalMap,half3x3(i.tangentWS.xyz,i.bitangentWS.xyz,i.normalWS.xyz));
                half3 normalVS = mul((half3x3)UNITY_MATRIX_V,normalWS);
                
                float2 normalUV = normalVS.xy * 0.5 + 0.5;

                half4 matcapMap = tex2D(_MatcapMap,normalUV);
                matcapMap += baseMap * matcapMap;

                c = lerp(baseMap,matcapMap,maskMap);
                return c;
            }
            ENDCG
        }
    }

    // SubShader
    // {
    //     Tags { "Queue"="Geometry" "RenderType" = "Opaque" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" }
    //     LOD 100

    //     Pass
    //     {
    //         Name "Unlit"
    //         HLSLPROGRAM
    //         // Required to compile gles 2.0 with standard srp library
    //         #pragma prefer_hlslcc gles
    //         #pragma exclude_renderers d3d11_9x
    //         #pragma vertex vert
    //         #pragma fragment frag
    //         #pragma multi_compile_fog
    //         #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
    //         #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    //         #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    //         #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"


    //         struct Attributes
    //         {
    //             float4 positionOS       : POSITION;
    //             float2 uv               : TEXCOORD0;
    //             half3 normalOS          : NORMAL;
    //             half4 tangentOS         : TANGENT;
    //         };

    //         struct Varyings
    //         {
    //             float4 positionCS       : SV_POSITION;
    //             float4 uv               : TEXCOORD0;
    //             half4 normalWS           : TEXCOORD1;
    //             half4 tangentWS         : TEXCOORD2;
    //             half4 bitangentWS       : TEXCOORD3;
    //         };

    //         CBUFFER_START(UnityPerMaterial)
    //         half4 _BaseColor;
    //         float4 _BaseMap_ST,_NormalMap_ST;
    //         CBUFFER_END
    //         TEXTURE2D (_BaseMap);SAMPLER(sampler_BaseMap);
    //         TEXTURE2D (_MatcapMap);SAMPLER(sampler_MatcapMap);
    //         TEXTURE2D (_NormalMap);SAMPLER(sampler_NormalMap);
    //         TEXTURE2D (_MaskMap);SAMPLER(sampler_MaskMap);
    //         // #define smp _linear_clampU_mirrorV
    //         // SAMPLER(smp);

    //         Varyings vert(Attributes v)
    //         {
    //             Varyings o = (Varyings)0;

    //             o.positionCS = TransformObjectToHClip(v.positionOS.xyz);
    //             o.uv.xy = TRANSFORM_TEX(v.uv, _BaseMap);
    //             o.uv.zw = TRANSFORM_TEX(v.uv, _NormalMap);
    //             half3 normalWS = TransformObjectToWorldNormal(v.normalOS.xyz);

    //             o.normalWS.xyz = normalWS;
    //             o.tangentWS.xyz = TransformObjectToWorldDir(v.tangentOS.xyz);
    //             half sign = v.tangentOS.w * GetOddNegativeScale();
    //             o.bitangentWS.xyz = cross(o.normalWS.xyz,o.tangentWS.xyz);
    //             return o;
    //         }

    //         half4 frag(Varyings i) : SV_Target
    //         {
    //             half4 c;

    //             half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, i.uv.xy);

    //             half maskMap = SAMPLE_TEXTURE2D(_MaskMap, sampler_MaskMap, i.uv.xy).r;

    //             //计算法线纹理
    //             half3 normalMap = UnpackNormal(SAMPLE_TEXTURE2D(_NormalMap,sampler_NormalMap,i.uv.zw));
    //             half3 normalWS = mul(normalMap,half3x3(i.tangentWS.xyz,i.bitangentWS.xyz,i.normalWS.xyz));
    //             half3 normalVS = mul((half3x3)UNITY_MATRIX_V,normalWS);
                
    //             float2 normalUV = normalVS.xy * 0.5 + 0.5;

    //             half4 matcapMap = SAMPLE_TEXTURE2D(_MatcapMap, sampler_MatcapMap, normalUV);
    //             matcapMap += baseMap * matcapMap;

    //             c = lerp(baseMap,matcapMap,maskMap);
    //             return c;
    //         }
    //         ENDHLSL
    //     }
    // }

}
